<template>
    <div class="login_container">
        <div class="login_form"> 
            <h3 class="title">后台管理系统</h3>
            <el-form ref="formRef" :model="loginForm">
                <el-form-item>
                    <el-input
                        v-model="loginForm.username"
                        placeholder="请输入用户名"
                    >  
                        <template #prefix>
                           <icon-ep-user></icon-ep-user>     
                        </template>
                    </el-input>
                </el-form-item>  
                <el-form-item>
                    <el-input
                        v-model="loginForm.password"
                        placeholder="请输入密码"
                        type="password"
                        show-password
                    >  
                        <template #prefix>
                           <icon-ep-lock></icon-ep-lock>     
                        </template>
                    </el-input>
                </el-form-item>  
                <el-form-item>
                    <div class="remember">
                        <el-checkbox v-model="loginForm.rememberMe" label="记住我" size="large" />   
                        <el-text type="primary">忘记密码？</el-text> 
                    </div>    
                </el-form-item>
                <el-divider>
                    其他登录方式
                </el-divider>
                <div class="otherLogin">
                    <icon-ep-apple />
                    <icon-ep-ChromeFilled />
                    <icon-ep-cherry />    
                </div>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin" class="loginBtn">登录</el-button>    
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
    import {login} from "@/api/auth"
    import { setToken} from "@/utils/token"
    import { searchSelfRouter,searchSelfUserInfo} from "@/api/user"
    import {useMenuStore} from "@/stores/menu"
     import {useUserStore} from "@/stores/user"
    import {useRouter} from "vue-router"
    const menuStore = useMenuStore();
    const userStore = useUserStore();
    const router = useRouter();
    const loginForm = ref({
        username:"admin",
        password:"123456",
        rememberMe:false
    })
    async function handleLogin(){
        try {
            await login(loginForm.value)
            .then(
                data=>{
                    if(data.code == 200){
                       ElMessage.success(data.msg);    
                       //保存token
                        setToken("token",data.data);
                    }
                }
            )
            //查询当前用户能够访问的菜单
            await searchSelfRouter().then( menuData => {
                console.log("菜单权限===="+menuData.data);
                menuStore.setMenuList(menuData.data);   
            }) 
            //设置动态路由，awiat同步，改代码所在的方法必须使用 async 关键字进行修饰，等待该方法运行结束
            await menuStore.addRouter();
            //查询当前登录的用户信息        
            await searchSelfUserInfo().then(data=>{
                console.log("当前登录的用户信息为:",data.data);
                userStore.setUserInfo(data.data);
            })
            //跳转到首页
            router.push("/index/first/index");
        } catch (error) {
            ElMessage.error(error);
        }

        
    }
</script>

<style lang="scss" scoped>
    .otherLogin{
        width: 100%;
        display: flex;
        justify-content: space-evenly;
    }
    .remember{
        width: 100%;
        display: flex;
        justify-content: space-between;
    }
    .login_container{
        background-image: url("@/assets/img/login_bg.jpg");
        background-size: 100% 100%;
        height: 100vh;
        display: flex;
        justify-content: flex-end;
        .login_form{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 50%;
            background-color: rgba(252, 206, 132, 0.353);
            .title{
                margin-bottom: 20px;
            }
            .el-form{
                width: 60%;
                .el-form-item{
                    width: 100%;
                }
                .loginBtn{
                    width: 100%;
                    margin: 10px auto;

                }
            }
        }
    }

</style>